/**
 * <p>Title: ServiceZMCE50.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2012-1-5 下午04:26:35  张小飞  Create
 *</pre>
 */
package com.baosight.sczx.zm.ce.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiBlockMeta;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ef.ui.tree.TreeConstants;
import com.baosight.iplat4j.ef.ui.tree.TreeService;
import com.baosight.iplat4j.ep.QueryMap;
import com.baosight.sczx.zm.ce.domain.ZMCEEquipmentClass;

/**
 * <p>Title: ServiceZMCE50.java</p>
 * <p>Description: 故障现象树</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2012-1-5 下午04:26:35  张小飞  Create
 *</pre>
 */
public class ServiceZMCE50 extends TreeService {
	
	private String fdKeyId = "root0";//设置根节点默认主键的值
	private String fdParentId = "no";//设置根节点默认的父节点值
	
	/**
	 * 
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE50</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-6上午11:26:27 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo query(EiInfo inInfo) {
		QueryMap queryInfo = QueryMap.getQueryMap(inInfo);
		String p = (String) queryInfo.get(TreeConstants.TREE_NODE);
		List children = new ArrayList();
		if (p == null) {
			p = "$";
		}
		if ("$".equals(p)) {
			children = this.getTopNodes();
		} else {
			children = this.getChildNodes(p);
		}

		EiInfo outInfo = new EiInfo();
		EiBlock block = outInfo.addBlock(p);
		block.setBlockMeta(initMetaData());
		block.setRows(children);
		return outInfo;
	}
	
	/**
	 * <p>Function: getTopNodes</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE50</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-5下午04:26:35 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List getTopNodes() {
		List _topNodes = new ArrayList();
		int i = 0;
		
		Map _tempMap = new HashMap();
	    _tempMap.put("label", fdKeyId);
	    _tempMap.put("text", "设备类别");		
		_tempMap.put("leaf", "0");
		_tempMap.put("fdToencode", "EQ");
		_topNodes.add(_tempMap);
		
		_tempMap = new HashMap();
		_tempMap.put("fdKeyId", fdKeyId);
		List checkTopNodes = dao.query("ZMCEEquipmentClass.checkTopNodes", _tempMap);//根节点数据校验是否存在根节点
		
		if(checkTopNodes != null && checkTopNodes.size() >= 1){
			ZMCEEquipmentClass resultMap = (ZMCEEquipmentClass)checkTopNodes.get(0);
			String fdName = resultMap.getFdName();
			if(fdName == null || !fdName.equals("设备类别")) {
				_tempMap = new HashMap<String, String>();
				_tempMap.put("fdKeyId", fdKeyId);
				dao.delete("ZMCEEquipmentClass.delete", _tempMap);//名称不一致，删除数据
			}
			else
			{
				i++;
			}
		}
		
		if(i == 0)
		{    ///如果没有根节点默认添加根节点值
			_tempMap = new HashMap();
			_tempMap.put("fdKeyId", fdKeyId);//<!-- FD_KEY_ID -->
			_tempMap.put("fdCode", "EQ");
			_tempMap.put("fdName", "设备分类");
			_tempMap.put("fdParentId", fdParentId);
			_tempMap.put("fdToencode", "EQ");
			_tempMap.put("fdIsLeaf", "0");
			_tempMap.put("fdRemarks", "fdRemarks");
			dao.insert("ZMCEEquipmentClass.insert", _tempMap);
			
		}
		
		return _topNodes;
		
	}
	
	/**
	 * <p>Function: getChildNodes</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE50</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-5下午04:26:35 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List getChildNodes(String arg0) {
		Map _argMap = new HashMap();
		_argMap.put(TreeConstants.TREE_NODE, arg0);
		List childNodes = dao.query("ZMCEEquipmentClass.queryChildren", _argMap);
		return childNodes;
	}

	/**
	 * <p>Function: initMetaData</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE50</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-5下午04:26:35 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiBlockMeta initMetaData() {
		EiBlockMeta eiMetadata = new EiBlockMeta();
		
		EiColumn eiColumn = new EiColumn("label");
		eiColumn.setDescName("FD_KEY_ID");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("text");
		eiColumn.setDescName("模块名称");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdToencode");
		eiColumn.setDescName("内码");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
        
		
		eiColumn = new EiColumn("leaf");
		eiColumn.setDescName("是否叶子");
		eiColumn.setType(EiConstant.COLUMN_TYPE_NUMBER);
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		return eiMetadata;
	}

}
